package com.chen.gulimall.member.service.impl;

import com.chen.gulimall.member.dao.MemberLevelDao;
import com.chen.gulimall.member.entity.MemberLevelEntity;
import com.chen.gulimall.member.exception.PhoneException;
import com.chen.gulimall.member.exception.UsernameException;
import com.chen.gulimall.member.service.MemberLevelService;
import com.chen.gulimall.member.vo.MemberUserLoginVo;
import com.chen.gulimall.member.vo.MemberUserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.gulimallcommon.common.utils.PageUtils;
import com.chen.gulimallcommon.common.utils.Query;

import com.chen.gulimall.member.dao.MemberDao;
import com.chen.gulimall.member.entity.MemberEntity;
import com.chen.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveResigerMember(MemberUserRegisterVo vo) {
        MemberEntity memberEntity = new MemberEntity();
        MemberLevelEntity levelEntity = memberLevelDao.selectOne(new QueryWrapper<MemberLevelEntity>().eq("default_status", 1));
        memberEntity.setLevelId(levelEntity.getId());

        isUniquePhone(vo.getPhone());
        isUniqueUserName(vo.getUserName());
        memberEntity.setUsername(vo.getUserName());
        memberEntity.setMobile(vo.getPhone());
        memberEntity.setNickname(vo.getUserName());

        //将密码进行md5加密，默认盐值加入密文的中间，不用在数据库另外创建
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = passwordEncoder.encode(vo.getPassWord());
        memberEntity.setPassword(password);

        baseMapper.insert(memberEntity);
    }


    @Override
    public void isUniqueUserName(String userName) throws UsernameException{
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));

        if (count > 0) {
            throw new UsernameException();
        }

    }


    @Override
    public void isUniquePhone(String phone) throws PhoneException{

        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));

        if (count > 0) {
            throw new PhoneException();
        }
    }

    @Override
    public MemberEntity login(MemberUserLoginVo memberUserLoginVo) {
        String usernameOrPhone = memberUserLoginVo.getLoginacct();
        String password = memberUserLoginVo.getPassWord();

        MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", usernameOrPhone).or().eq("mobile", usernameOrPhone));
        if (entity == null) {
            return null;
        }

        boolean matches = new BCryptPasswordEncoder().matches(password, entity.getPassword());

        if (matches) {
            return entity;
        } else {
            return null;
        }
    }

}